﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;


namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OControlTicketList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除, Permission.导出 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OControlTicketList";
            }
        }
        #region 属性 (查询条件)

        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "OpenDate" : base.orderField;
            }
        }
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "Desc" : base.orderDirection;
            }
        }


        protected string ItemCode
        {
            get
            {
                return GetRequestValue<string>("ItemCode", "");

            }
        }
        protected string ControlReason
        {
            get
            {
                return GetRequestValue<string>("ControlReason", "");

            }
        }
        protected string SerialNumber1
        {
            get
            {
                return GetRequestValue<string>("SerialNumber1", "");

            }
        }

        protected string SupplierName
        {
            get
            {
                return GetRequestValue<string>("SupplierName", "");
            }
        }
        protected string TNO
        {
            get
            {
                return GetRequestValue<string>("TNO", "");
            }
        }

        protected string ApprovalDateFrom
        {
            get
            {
                return GetRequestValue<string>("ApprovalDateFrom", "");

            }
        }
        protected string ApprovalDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(ApprovalDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("ApprovalDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(ApprovalDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                    else
                        return todate;
                }

            }
        }

        #endregion
        protected string GetWhereSql()
        {
            string wheresql = " 1=1 ";
            if (!string.IsNullOrEmpty(ItemCode))
            {
                wheresql =string.Format( " (itemcode like '%{0}%' or itemname like '%{0}%') ",ItemCode);
            }

            if (!string.IsNullOrEmpty(SerialNumber1))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and SerialNumber1 like '%{0}%'", SerialNumber1);
                else
                    wheresql = string.Format(" SerialNumber1 like '%{0}%'", SerialNumber1);
            }

            if (!string.IsNullOrEmpty(TNO))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and TNO  ='{0}'", TNO);
                else
                    wheresql = string.Format(" TNO  ='{0}'", TNO);
            }
            if (!string.IsNullOrEmpty(ControlReason))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and ControlReason like '%{0}%'", ControlReason);
                else
                    wheresql = string.Format(" ControlReason like '%{0}%'", ControlReason);
            }
            if (!string.IsNullOrEmpty(SupplierName))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and SupplierName like '%{0}%'", SupplierName);
                else
                    wheresql = string.Format(" SupplierName like '%{0}%'", SupplierName);
            }

            string DOWNSQL = "";
            if (!string.IsNullOrEmpty(ApprovalDateFrom))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("ApprovalDate", QueryOperatorsEnum.范围, ApprovalDateFrom + "|" + ApprovalDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            return wheresql;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            this.AddLog(Permission.查看, GetWhereSql());
        }

        protected void DataBind()
        {
            this.rpMenu.DataSource = OControlTicketController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataBind();
        }
        public string GetControlReason(string controlname, string selectValue)
        {
            string[] ControlReasonArr = new string[] { "试制", "量试", "初量产", "国产化开发", "厂商变更", "设变管制", "新机型开发", "特别管制" };
            string checkboxtemp = "<option value=\"{0}\" {1} >{0}</option>";
            string returnHtml = string.Format("<select  name=\"{0}\"><option value='' {1}>显示所有</option>", controlname, selectValue==""?"selected":"");
            foreach (string item in ControlReasonArr)
            {
                string c = "";
                foreach (string tt in selectValue.Split(new char[] { ',' }))
                {
                    if (tt == item)
                    {
                        c = "selected";
                        break;
                    }
                }
                returnHtml += string.Format(checkboxtemp,item, c);
            }
            returnHtml += "</select>";
            return returnHtml;
        }
    }
}